﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Web;

namespace Common.IO
{
    public class Stream
    {
        /// <summary>
        /// 将接收的post数据转换为Object，这个对象必须是Serializable的
        /// </summary>
        /// <returns></returns>
        public object GetObjectFromStream()
        {
            System.IO.Stream stream = HttpContext.Current.Request.InputStream;
            return GetObjectFromStream(stream);
        }
        /// <summary>
        /// 将接收的post数据转换为Object，这个对象必须是Serializable的
        /// </summary>
        /// <returns></returns>
        public object GetObjectFromStream(System.IO.Stream stream)
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            return (object)bFormatter.Deserialize(stream);
        }

        /// <summary>
        /// 把流转换成字节数组
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public byte[] StreamToBytes(System.IO.Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            stream.Flush();
            stream.Close();
            stream.Dispose();
            return bytes;
        }
        /// <summary>
        /// 把字节数组转换成流
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public System.IO.Stream BytesToStream(byte[] bytes)
        {
            System.IO.Stream stream = new MemoryStream(bytes);
            return stream;
        }

        /// <summary>
        /// 把接收到的数据流转换成文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        public void StreamToFile(System.IO.Stream stream, string fileName)
        {
            byte[] bytes = StreamToBytes(stream);
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }

        /// <summary>
        /// 把接收到的数据流转换成文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        public void BytesToFile(byte[] bytes, string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }

        /// <summary> 
        /// 从文件读取 Stream 
        /// </summary> 
        public System.IO.Stream FileToStream(string fileName)
        {
            byte[] bytes = FileToBytes(fileName);
            System.IO.Stream stream = new MemoryStream(bytes);
            return stream;
        }

        /// <summary> 
        /// 从文件读取bytes[]
        /// </summary> 
        public byte[] FileToBytes(string fileName)
        {
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            return bytes;
        }
        /// <summary>
        /// 字节数组转换成object
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public object DeserializeBinary(byte[] buf)
        {
            System.IO.MemoryStream memStream = new MemoryStream(buf);
            memStream.Position = 0;
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            object newobj = deserializer.Deserialize(memStream);
            memStream.Close();
            return newobj;
        }

        /// <summary>
        /// 序列化为二进制字节数组  
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public byte[] SerializeBinary(object obj)
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            serializer.Serialize(memStream, obj);
            return memStream.GetBuffer();
        }
    }
}
